package com.liqy.api.servlet;

//@Slf4j
//@Component
public class APIDemoServlet /*extends HttpServlet*/ {

   /* @Value("${apiDemo.api.context}")
    String apiContext;

    @Autowired
    RouterCache routerCache;

    @Autowired
    private GroovyScriptFactory groovyScriptFactory;

    @Autowired
    RouterCacheController routerCacheController;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.debug("servlet execute");
        String servletPath = request.getRequestURI();
        servletPath = servletPath.substring(apiContext.length() + 1);
        response.setHeader(Header.CONTENT_TYPE.toString(), "application/json");

        PrintWriter out = null;
        try {
            out = response.getWriter();
            Response Response = process(servletPath, request, response);
            out.append(JSON.toJSONString(Response));

        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.append(JSON.toJSONString(Response.errResponse(e.toString())));
            log.error(e.toString(), e);
        } finally {
            if (out != null)
                out.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

    public Response process(String path, HttpServletRequest request, HttpServletResponse response) throws IOException {

        if (!path.contains("/api/")) {
//            // 校验接口是否存在
            Object scriptStr = routerCache.get(path);
            if (scriptStr == null) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                return Response.errResponse("Api not exists");
            }

            JSONObject args = ServletUtil.getRequestJson(request);
            Object o = groovyScriptFactory.scriptGetAndRun(String.valueOf(scriptStr), new Binding(args));
            return Response.successResponse("doSuccess", String.valueOf(o));
        } else {
            switch (path) {
                *//*case "/api/add": {
                    RouterInfo routerInfo = JSON.toJavaObject(ServletUtil.getRequestJson(request), RouterInfo.class);
                    return routerCacheController.addCache(routerInfo);
                }*//*
                case "/api/set": {
                    RouterInfo routerInfo = JSON.toJavaObject(ServletUtil.getRequestJson(request), RouterInfo.class);
                    return routerCacheController.updateCache(routerInfo);
                }
                case "/api/rm": {
                    JSONObject requestJson = ServletUtil.getRequestJson(request);
                    return routerCacheController.cleanCache(String.valueOf(requestJson.get("url")));
                }
                case "/api/add_all": {
                    JSONObject requestJson = ServletUtil.getRequestJson(request);
                    return routerCacheController.addAllCache(requestJson);
                }
                case "/api/view": {
                    JSONObject requestJson = ServletUtil.getRequestJson(request);
                    return routerCacheController.viewCache(String.valueOf(requestJson.get("url")));
                }
                case "/api/view_all": {
                    return routerCacheController.viewAllCache();
                }
                default: {
                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                    return Response.errResponse("Api not exists");
                }
            }
        }
    }*/

}
